﻿using System;
using System.Workflow.Activities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.WorkflowActions;
using Microsoft.SharePoint;

namespace TPG.SharePoint.Workflow.Sample.Workflow
{
    public sealed partial class Workflow : SequentialWorkflowActivity
    {
        public string TaskOneResult;
        public string TaskOneComments;

        public Guid TaskId = default(Guid);

        public SPWorkflowActivationProperties WorkflowActivationProperties;
        public SampleConfiguration _configData;

        public Workflow()
        {
            InitializeComponent();
        }

        private void OnWorkflowActivated_Invoked(object sender, ExternalDataEventArgs e)
        {
            _configData = WorkflowDataSerializer.DeserializeFromXmlString<SampleConfiguration>(
                WorkflowActivationProperties.InitiationData);
            EnsureTaskContentType(ContentTypeID.TaskTwo);
        }

        private void CreateTaskOne_Invoking(object sender, EventArgs e)
        {
            TaskId = Guid.NewGuid();
            CreateTask activity = sender as CreateTask;

            activity.TaskProperties = new SPWorkflowTaskProperties();
            activity.TaskProperties.AssignedTo = _configData.Level1Approver;
            activity.TaskProperties.TaskType = 0;
            activity.TaskProperties.DueDate = DateTime.Now.AddDays(3);
            activity.TaskProperties.ExtendedProperties["Instructions"] = _configData.Instructions;
            activity.TaskProperties.Title = "Perform Task One";
        }

        private void ChangeTaskOne_Invoked(object sender, ExternalDataEventArgs e)
        {
            SPTaskServiceEventArgs args = e as SPTaskServiceEventArgs;
            TaskOneResult = (string)args.afterProperties.ExtendedProperties["WSS3WorkflowSampleReviewStatus"];
            TaskOneComments = (string)args.afterProperties.ExtendedProperties["WSS3WorkflowSampleReviewComments"];
        }     

        private void WorkflowModification_Invoking(object sender, EventArgs e)
        {
            EnableWorkflowModification activity = sender as EnableWorkflowModification;

            InstructionsModification data = new InstructionsModification();
            data.Instructions = _configData.Instructions;

            ModificationData typedData = ModificationData.CreateUsingTypedData(data);
            activity.ContextData = typedData.NativeData;
        }

        private void WaitForModification_Invoked(object sender,
            ExternalDataEventArgs e)
        {
            SPModificationEventArgs args = e as SPModificationEventArgs;
            ModificationData data = ModificationData.CreateUsingStringData(args.data);
            InstructionsModification typedData = data.GetTypedData<InstructionsModification>();
            // ...
        }
        void EnsureTaskContentType(string contentTypeIDString)
        {
            SPList taskList = WorkflowActivationProperties.TaskList;
            if (taskList.ContentTypesEnabled == false)
            {
                taskList.ContentTypesEnabled = true;
                taskList.Update();
            }
            SPContentTypeId contentTypeID = new SPContentTypeId(contentTypeIDString);
            SPContentTypeId activatedID = taskList.ContentTypes.BestMatch(contentTypeID);
            if (activatedID == null || activatedID.IsChildOf(contentTypeID) == false)
            {
                SPContentType contentType = WorkflowActivationProperties.Web.AvailableContentTypes[contentTypeID];
                taskList.ContentTypes.Add(contentType);
            }
        }

    }
}
